package cn.jason.ymcc.service.impl;

import cn.jason.ymcc.domain.CourseType;
import cn.jason.ymcc.dto.CourseMbxDto;
import cn.jason.ymcc.mapper.CourseMapper;
import cn.jason.ymcc.mapper.CourseTypeMapper;
import cn.jason.ymcc.result.JSONResult;
import cn.jason.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author jason
 * @since 2022-07-20
 */
@Service
@Slf4j
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private CourseTypeMapper courseTypeMapper;


    @Override
    @Cacheable(cacheNames = "CourseType",key = "'tree'")
    public List<CourseType> treeData() {
        log.info("走mysql查询返回");
        List<CourseType> tree = tree();
        return tree;
    }

    @Override
    public JSONResult crumbs(Long courseTypeId) {
        List<CourseMbxDto> returnList = new ArrayList<>();
        //根据当前课程分类ID查询课程分类详情
        CourseType courseType = courseTypeMapper.selectById(courseTypeId);
        //得到id的path
        String[] split = courseType.getPath().split("\\.");
        //根据集合批量查询出多个分类对象
        List<CourseType> courseTypes = courseTypeMapper.selectBatchIds(Arrays.asList(split));
        //循环分类
        for (CourseType coursetype:courseTypes
        ) {
            CourseMbxDto mbxDto = new CourseMbxDto();
            mbxDto.setOwnerProductType(coursetype);
            Wrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",coursetype.getPid())
                    .ne("id",coursetype.getId());
            List<CourseType> course = courseTypeMapper.selectList(wrapper);
            mbxDto.setOtherProductTypes(course);
            returnList.add(mbxDto);
        }
        return JSONResult.success(returnList);
    }

    /**
     * 查询的数据放到内存
     * 封装无限极树
     * @return
     */
    private List<CourseType> tree() {
        List<CourseType> courseTypes = selectList(null);
        //stream
        Map<Long, CourseType> allTypes = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, dept -> dept));
        //返回List
        List<CourseType> firstList = new ArrayList<>();
        for (CourseType type:courseTypes){
            if(type.getPid() == null || type.getPid().intValue() == 0){//第一层级
                firstList.add(type);
            }else{
                //不是第一层级找上级
                allTypes.get(type.getPid()).getChildren().add(type);
            }
        }
        return firstList;
}

    @Override
    @CacheEvict(cacheNames="coursetype", key = "'tree'")
    public boolean insert(CourseType entity) {
        boolean insert = super.insert(entity);
        return insert;
    }

    @Override
    @CacheEvict(cacheNames="coursetype", key = "'tree'")
    public boolean updateById(CourseType entity) {
        boolean update = super.updateById(entity);
        return update;
    }

    @Override
    @CacheEvict(cacheNames="coursetype", key = "'tree'")
    public boolean deleteById(Serializable id) {
        boolean delete = super.deleteById(id);
        return delete;
    }

}
